using System;
using SMS.SMPPClient.Util;
using SMS.SMPPClient.PDU.Incoming;

namespace SMS.SMPPClient.PDU.Outgoing
{
    /// <summary>
    /// Description of SMPPSubmitMultiResponse.
    /// </summary>
    public class SMPPSubmitMultiResponse : SMPPSubmitSMResponse
    {
        private byte _NumberUnsuccessful;
        private UnsuccessAddress[] _UnsuccessfulAddresses;

        /// <summary>
        /// The number of messages that could not be delivered.
        /// </summary>
        public byte NumberUnsuccessful
        {
            get
            {
                return _NumberUnsuccessful;
            }
        }

        /// <summary>
        /// The array of unsuccessful addresses.
        /// </summary>
        public UnsuccessAddress[] UnsuccessfulAddresses
        {
            get
            {
                return (UnsuccessAddress[])_UnsuccessfulAddresses.Clone();
            }
        }

        /// <summary>
        /// Creates a submit_multi response PDU.
        /// </summary>
        /// <param name="response">The byte response from the
        /// SMSC.</param>
        public SMPPSubmitMultiResponse(byte[] response)
            : base(response)
        {
            DecodeSMSCResponse();
        }

        /// <summary>
        /// Decodes the submit_multi response from the SMSC.
        /// </summary>
        protected override void DecodeSMSCResponse()
        {
            DecodeNonTLV();
            byte[] remainder = base.ResponseAfterMsgID;

            //the SMSC might not send back the number of unsuccessful messages,
            //so check if it did
            if (remainder.Length > 0)
            {
                _NumberUnsuccessful = remainder[0];

                _UnsuccessfulAddresses = new UnsuccessAddress[NumberUnsuccessful];

                byte[] remainder2 = ByteArrayCopy(remainder, 1, remainder.Length - 1);
                remainder = remainder2;
                remainder2 = null;
                //unsuccessful
                for (int i = 0; i < UnsuccessfulAddresses.Length; i++)
                {
                    _UnsuccessfulAddresses[i] = new UnsuccessAddress(ref remainder);
                }
            }

            //fill the TLV table if applicable
            tlvTable.TranslateTLVDataIntoTable(remainder);
        }
    }
}
